﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.MicroKernel;

namespace MyWeb.WebUI.Tools
{
	public class WindsorModelBinder<T> : DefaultModelBinder
	{
		private readonly IKernel kernel;

		public WindsorModelBinder(IKernel kernel)
		{
			this.kernel = kernel;
		}

		public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
		{
			IModelBinder binder = kernel.Resolve(typeof(T)) as IModelBinder;
			if (binder != null)
			{
				return binder.BindModel(controllerContext, bindingContext);
			}
			return base.BindModel(controllerContext, bindingContext);
		}
	}
}